<?php

require_once 'Properties.php';
require_once 'Items.php';

class ItemProperties {

    private $_ID;
    private $_itemID;
    private $_propertyID;
    private $_value;
    private $_item;
    private $_property;

    public function getID() {
        return $this->_ID;
    }

    public function setID($ID) {
        $this->_ID = $ID;
    }

    public function getItemID() {
        return $this->_itemID;
    }

    public function setItemID($ItemID) {
        $this->_itemID = $ItemID;
    }

    public function getPropertyID() {
        return $this->_propertyID;
    }

    public function setPropertyID($PropertyID) {
        $this->_propertyID = $PropertyID;
    }

    public function getValue() {
        return $this->_value;
    }

    public function setValue($Value) {
        $this->_value = $Value;
    }

    public function getItem() {
        return $this->_item;
    }

    public function setItem($Item) {
        $this->_item = $Item;
    }

    public function getProperty() {
        return $this->_property;
    }

    public function setProperty($Property) {
        $this->_property = $Property;
    }

    // add a property of item into item_properties table
    public function add($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `item_properties`
                                (
                                 `IP_I_ID`,
                                 `IP_P_ID`,
                                 `IP_Value`)
                    VALUES (
                            :IP_I_ID,
                            :IP_P_ID,
                            :IP_Value);";

            $parameters = array(
                ':IP_I_ID', ':IP_P_ID',
                ':IP_Value');

            $values = array(
                $this->getItemID(), $this->getPropertyID(),
                $this->getValue());

            $types = array(
                PDO::PARAM_INT, PDO::PARAM_INT,
                PDO::PARAM_STR);

            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    // get property list of item
    public function getItemPropertiesByItemID($db = null) {
        if ($db != null) {
            $sql = "SELECT
                        `IP_ID`,
                        `IP_I_ID`,
                        `IP_P_ID`,
                        `IP_Value`
                      FROM `item_properties`
                      WHERE IP_I_ID = :IP_I_ID";

            $parameters = array(':IP_I_ID');
            $values = array($this->getItemID());
            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {

                // mảng danh sách trả về
                $rows = array();

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {
                    $itemProperties = new ItemProperties();
                    $itemProperties->setID($row->IP_ID);
                    $itemProperties->setItemID($row->IP_I_ID);
                    $itemProperties->setPropertyID($row->IP_P_ID);
                    $itemProperties->setValue($row->IP_Value);

                    $property = new Properties();
                    $property->setID($itemProperties->getPropertyID());
                    $property = $property->getByID($db);

                    $itemProperties->setProperty($property);

                    $rows[] = $itemProperties;
                }
                return $rows;
            } else {
                return null;
            }
        }
    }

    // get property list of item
    public function getItemPropertyByID($db = null) {
        if ($db != null) {
            $sql = "SELECT
                        `IP_ID`,
                        `IP_I_ID`,
                        `IP_P_ID`,
                        `IP_Value`
                      FROM `item_properties`
                      WHERE IP_ID = :IP_ID";

            $parameters = array(':IP_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {
                $row = $result->fetch();
                $this->setID($row->IP_ID);
                $this->setItemID($row->IP_I_ID);
                $this->setPropertyID($row->IP_P_ID);
                $this->setValue($row->IP_Value);

                $property = new Properties();
                $property->setID($this->getPropertyID());
                $property = $property->getByID($db);

                $this->setProperty($property);

                return $this;
            } else {
                return null;
            }
        }
    }

    // delete property list of an item
    public function deleteByItemID($db = null) {
        if ($db != null) {
            $sql = "DELETE
                        FROM `item_properties`
                        WHERE `IP_I_ID` = :IP_I_ID;";

            $parameters = array(':IP_I_ID');

            $values = array($this->getItemID());

            $types = array(PDO::PARAM_INT);

            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    // get property list (include ID and Name) of an item
    public function getListGroupByValue($db) {
        if ($db != null) {
            $sql = "SELECT `IP_ID`,`IP_I_ID`,`IP_P_ID`,`IP_Value`  FROM `item_properties` 
                    GROUP BY IP_Value ORDER BY IP_P_ID";

            $parameters = null;
            $values = null;
            $types = null;

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {

                // mảng danh sách trả về
                $rows = array();

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {

                    $itemProperties = new ItemProperties();
                    $itemProperties->setID($row->IP_P_ID);
                    $itemProperties->setItemID($row->IP_I_ID);
                    $itemProperties->setPropertyID($row->IP_P_ID);
                    $itemProperties->setValue($row->IP_Value);


                    $property = new Properties();
                    $property->setID($itemProperties->getPropertyID());
                    $property = $property->getByID($db);
                    $itemProperties->setProperty($property);

                    $item = new Items();
                    $item->setID($itemProperties->getItemID());
                    $item = $item->getByID($db);
                    $itemProperties->setItem($item);




                    $rows[] = $itemProperties;
                }
                return $rows;
            } else {
                return null;
            }
        }
    }

}

?>
